package com.itheima.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.Result;
import com.itheima.pojo.HotSetmealVo;
import com.itheima.pojo.ReportDataVo;
import com.itheima.service.ReportService;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

@RequestMapping("/report")
@RestController
public class ReportController {

    @Reference
    ReportService reportService;

    @RequestMapping("/getMemberReport")
    public Result getMemberReport(){
        DateTime dateTime = DateUtil.offsetMonth(new Date(), -12);

        List<String> months = new ArrayList<>();

        for (int i = 3; i < 6; i++) {
            DateTime dateTime1 = DateUtil.offsetMonth(dateTime, i);
            months.add(dateTime1.toString("yyyy-MM"));
        }


        List<Integer>  memberCount = reportService.getMemberReport(months);

        Map map = new HashMap<>();
        map.put("months",months);
        map.put("memberCount",memberCount);

        return Result.success("",map);
    }

    @RequestMapping("/getSetmealReport")
    public Result getSetmealReport(){
        List<Map> maps =  reportService.getSetmealReport();
        return Result.success("",maps);
    }


    @RequestMapping("/getBusinessReportData")
    public Result getBusinessReportData(){
         ReportDataVo vo = reportService.getBusinessReportData();
        return Result.success("",vo);
    }


    @RequestMapping("/exportBusinessReport")
    public void exportBusinessReport(HttpServletResponse response, HttpServletRequest request) throws IOException {
        ReportDataVo vo = reportService.getBusinessReportData();
        //加载excel
        String realPath = request.getSession().getServletContext().getRealPath("template");
        XSSFWorkbook workbook = new XSSFWorkbook(realPath + "/report_template.xlsx");
        //获取sheet
        XSSFSheet sheetAt = workbook.getSheetAt(0);

        //获取row  //获取cell
        sheetAt.getRow(2).getCell(5).setCellValue(vo.getReportDate());

        sheetAt.getRow(4).getCell(5).setCellValue(vo.getTodayNewMember());
        sheetAt.getRow(4).getCell(7).setCellValue(vo.getTotalMember());

        sheetAt.getRow(5).getCell(5).setCellValue(vo.getThisWeekNewMember());
        sheetAt.getRow(5).getCell(7).setCellValue(vo.getThisMonthNewMember());



        sheetAt.getRow(7).getCell(5).setCellValue(vo.getTodayOrderNumber());
        sheetAt.getRow(7).getCell(7).setCellValue(vo.getTodayVisitsNumber());
        sheetAt.getRow(8).getCell(5).setCellValue(vo.getThisWeekOrderNumber());
        sheetAt.getRow(8).getCell(7).setCellValue(vo.getThisWeekVisitsNumber());
        sheetAt.getRow(9).getCell(5).setCellValue(vo.getThisMonthOrderNumber());
        sheetAt.getRow(9).getCell(7).setCellValue(vo.getThisMonthVisitsNumber());


        List<HotSetmealVo> hotSetmeal = vo.getHotSetmeal();


        int i = 12;

        for (HotSetmealVo hotSetmealVo : hotSetmeal) {
            sheetAt.getRow(i).getCell(4).setCellValue(hotSetmealVo.getName());
            sheetAt.getRow(i).getCell(5).setCellValue(hotSetmealVo.getSetmealCount());
            sheetAt.getRow(i).getCell(6).setCellValue(hotSetmealVo.getProportion().doubleValue());
            i++;
        }

        //告诉浏览器现在响应的是excel
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");//application/json
        response.setHeader("Content-Disposition","attachment;filename="+ java.net.URLEncoder.encode("运营数据报表", "UTF-8")+".xlsx");

        ServletOutputStream outputStream = response.getOutputStream();
        workbook.write(outputStream);

        outputStream.flush();
        outputStream.close();
        workbook.close();
    }



    public static void main(String[] args) {
        DateTime dateTime = DateUtil.offsetMonth(new Date(), -12);
//        System.out.println(dateTime.toString());

        List<String> months = new ArrayList<>();

        for (int i = 0; i < 12; i++) {
            DateTime dateTime1 = DateUtil.offsetMonth(dateTime, i);
            months.add(dateTime1.toString("yyyy-MM"));
        }
        System.out.println(months);
    }
}
